package com.wye.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wye.dao.BrandMapper;
import com.wye.dao.CategoryMapper;
import com.wye.goods.pojo.Brand;
import com.wye.service.IBrandService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BrandServiceImpl implements IBrandService {

    private static Logger logger = LoggerFactory.getLogger(BrandServiceImpl.class);

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Brand> findByCategoryId(Integer categoryId) {
        return brandMapper.findByCategoryId(categoryId);
    }

    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }

    @Override
    public Brand findById(Integer id) {
        if (id != null){
            return brandMapper.selectByPrimaryKey(id);
        }
        return null;
    }

    @Override
    public int insertBrand(Brand brand) {
        return brandMapper.insertSelective(brand);
    }

    @Override
    public void updateBrand(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public void deleteBrand(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Brand> findList(Brand brand) {
        List<Brand> brandList = new ArrayList<>();
        if (brand != null){
            if (!StringUtils.isEmpty(brand.getName())){
                String name = "%" + brand.getName() + "%";
                logger.info("name条件={}", name);
                brandList = brandMapper.selectByLikeName(name);
            }
            if (!StringUtils.isEmpty(brand.getLetter())){
                brandList = brandMapper.selectByLikeLetter("%" + brand.getLetter() + "%");
            }
            if (!StringUtils.isEmpty(brand.getName()) && !StringUtils.isEmpty(brand.getLetter())){
                brandList = brandMapper.selectByLikeLetterOrName(brand.getLetter(), brand.getName());
            }

        }
        return brandList;

    }

    @Override
    public PageInfo<Brand> findPage(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Brand> brandList = brandMapper.selectAll();
        //封装PageInfo
        return new PageInfo<>(brandList);
    }

    @Override
    public PageInfo<Brand> findPage(Integer page, Integer size, Brand brand) {
        //分页
        PageHelper.startPage(page, size);
        //搜索数据
        List<Brand> brandList = findList(brand);
        //封装PageInfo
        return new PageInfo<Brand>(brandList);
    }


}
